package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.ComDischargePermitMapper;
import com.bf.electroplating.pojo.dto.pollution.PollutionPermitDTO;
import com.bf.electroplating.pojo.dto.pollution.PollutionPermitQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComDischargePermit;
import com.bf.electroplating.pojo.entity.PubAttachment;
import com.bf.electroplating.pojo.entity.PubAttachmentRel;
import com.bf.electroplating.pojo.enums.AttModuleEnums;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.pollution.PollutionPermitVO;
import com.bf.electroplating.service.ComCompanyService;
import com.bf.electroplating.service.ComDischargePermitService;
import com.bf.electroplating.service.PubAttachmentService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 15451
 * @description 针对表【com_discharge_permit(排污许可证表)】的数据库操作Service实现
 * @createDate 2025-05-09 11:11:32
 */
@Service
public class ComDischargePermitServiceImpl extends ServiceImpl < ComDischargePermitMapper,ComDischargePermit > implements ComDischargePermitService {

	@Resource
	private ComCompanyService comCompanyService;

	@Resource
	private PubAttachmentService pubAttachmentService;

	@Override
	public ResultVO < Page < PollutionPermitVO > > pageList ( PollutionPermitQueryDTO queryDTO ) {
		Page < ComDischargePermit > page = new Page <> ( queryDTO.getPageNum () , queryDTO.getPageSize () );
		Map < String,String > companyMap =
				comCompanyService.list ().stream ().collect ( Collectors.toMap ( ComCompany::getId ,
						ComCompany::getCompanyName ) );

		LambdaQueryWrapper < ComDischargePermit > wrapper = new LambdaQueryWrapper <> ();
		if ( StringUtils.isNotBlank ( queryDTO.getCompanyId () ) ) {
			wrapper.eq ( ComDischargePermit::getCompanyId , queryDTO.getCompanyId () );
		}
		if ( StringUtils.isNotBlank ( queryDTO.getBasicInfo () ) ) {
			wrapper.like ( ComDischargePermit::getBasicInfo , queryDTO.getBasicInfo () );
		}
		wrapper.orderByDesc ( ComDischargePermit::getCreateTime );

		Page < ComDischargePermit > resultPage = this.page ( page , wrapper );

		// 转换为VO
		Page < PollutionPermitVO > voPage = new Page <> ();
		BeanUtils.copyProperties ( resultPage , voPage , "records" );

		List < PollutionPermitVO > voList = new ArrayList <> ();
		for ( ComDischargePermit entity : resultPage.getRecords () ) {
			PollutionPermitVO vo = new PollutionPermitVO ();
			BeanUtils.copyProperties ( entity , vo );
			vo.setExpiryTime ( entity.getExpirationTime () );
			vo.setCompanyName ( companyMap.get ( entity.getCompanyId () ) );
			vo.setOriginalFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.ORIGINAL.getCode () ) );
			vo.setDuplicateFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.DUPLICATE.getCode () ) );
			voList.add ( vo );
		}
		voPage.setRecords ( voList );

		return ResultVO.ok ( voPage );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > saveData ( PollutionPermitDTO dto ) {
		ComDischargePermit permit = new ComDischargePermit ();
		BeanUtils.copyProperties ( dto , permit );
		permit.setExpirationTime ( dto.getExpiryTime () );
		saveOrUpdate ( permit );

		pubAttachmentService.saveWholeAttachmentRel ( dto.getOriginalFile () , permit.getId () , AttModuleEnums.ORIGINAL.getCode () );
		pubAttachmentService.saveWholeAttachmentRel ( dto.getDuplicateFile () , permit.getId () , AttModuleEnums.DUPLICATE.getCode () );

		return ResultVO.ok ( null , "保存成功" );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > delete ( List < String > ids ) {
		if ( ids == null || ids.isEmpty () ) {
			return ResultVO.fail ( "ID不能为空" );
		}

		this.removeByIds ( ids );
		return ResultVO.ok ( null , "删除成功" );
	}

	@Override
	public ResultVO < PollutionPermitVO > getDetail ( String id ) {
		if ( StringUtils.isBlank ( id ) ) {
			return ResultVO.fail ( "ID不能为空" );
		}
		ComDischargePermit entity = this.getById ( id );
		if ( entity == null ) {
			return ResultVO.fail ( "记录不存在" );
		}

		Map < String,String > companyMap = comCompanyService.list ().stream ().collect ( Collectors.toMap ( ComCompany::getId , ComCompany::getCompanyName ) );

		PollutionPermitVO vo = new PollutionPermitVO ();
		BeanUtils.copyProperties ( entity , vo );
		vo.setExpiryTime ( entity.getExpirationTime () );
		vo.setCompanyName ( companyMap.get ( entity.getCompanyId () ) );
		vo.setOriginalFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.ORIGINAL.getCode () ) );
		vo.setDuplicateFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.DUPLICATE.getCode () ) );

		return ResultVO.ok ( vo );
	}

}




